

/**
 * 设置cookie
 *
 * @param {*} name
 * @param {*} value
 * @param {*} day
 */
export const setCookie = (name, value, day) => {
  const date = new Date();
  date.setDate(date.getDate() + day);
  document.cookie = `${name}=${value};expires=${date}`;
};

/**
 * 获取cookie
 *
 * @param {*} name
 * @returns
 */

export const getCookie = name => {
  const reg = RegExp(`${name}=([^;]+)`);
  const arr = document.cookie.match(reg);
  if (arr) {
    return arr[1];
  }
  return "";
};

/**
 * 删除 cookie
 *
 * @param {*} name
 */
export const delCookie = name => {
  setCookie(name, null, -1);
};

/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  localStorage.setItem(name, content);
};

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return;
  let value = localStorage.getItem(name);
  if (value && value.indexOf("{") >= 0) {
    value = JSON.parse(localStorage.getItem(name));
  }
  // eslint-disable-next-line consistent-return
  return value;
};

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return;
  localStorage.removeItem(name);
};

/**
 * 删除所有localStorage
 */
export const clearStore = () => {
  localStorage.clear();
};


//操作cookie
export const DRcookie = {
  set(key, val, time, domain) { //设置cookie方法
    const regex = /[\u3400-\u4DB5\u4E00-\u9FEF\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29\u{20000}-\u{2A6D6}\u{2A700}-\u{2B734}\u{2B740}-\u{2B81D}\u{2B820}-\u{2CEA1}\u{2CEB0}-\u{2EBE0}]/u
    //判断是否含有中文正则(兼容srfair浏览器)
    if(regex.test(val)){
      val = escape(val)
    }
    var date = new Date(); //获取当前时间
    var expiresDays = time; //将date设置为n天以后的时间
    date.setTime(date.getTime() + expiresDays * 1 * 3600 * 1000); //格式化为cookie识别的时间
    var temp = key + "=" + val + ";expires=" + date.toGMTString(); //设置cookie
    if(domain){
      temp += ';domain=' + domain
    }
    document.cookie = temp
  },
  get(key) { //获取cookie方法
    /*获取cookie参数*/
    var getCookie = document.cookie.replace(/[ ]/g, ""); //获取cookie，并且将获得的cookie格式化，去掉空格字符
    var arrCookie = getCookie.split(";") //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
    var tips; //声明变量tips
    for (var i = 0; i < arrCookie.length; i++) { //使用for循环查找cookie中的tips变量
      var arr = arrCookie[i].split("="); //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
      if (key == arr[0]) { //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
        tips = arr[1]; //将cookie的值赋给变量tips
        break; //终止for循环遍历
      }
    }
    return unescape(tips);
  },
  delete(key,domain) { //删除cookie方法
    var date = new Date(); //获取当前时间
    date.setTime(date.getTime() - 10000); //将date设置为过去的时间
    if(domain){
      document.cookie = key + "=v; expires =" + date.toGMTString()+';domain='+ domain + ';path=/;'; //设置cookie
    }else{
      document.cookie = key + "=v; expires =" + date.toGMTString(); //设置cookie
    }
  },
  clearUserInfoAll(){
    this.delete("tH");
    this.delete("tF");
    window.localStorage.userInfo = '{}'
  }
}